﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.OtherForms
{
    public partial class ThemHangHoaForm : Form
    {
        private string _maHangHoa = "";

        public ThemHangHoaForm()
        {
            InitializeComponent();
        }

        public ThemHangHoaForm(string maHangHoa)
        {
            InitializeComponent();
            _maHangHoa = maHangHoa;
            this.Text = "Chỉnh sửa thông tin hàng hóa";
        }

        private void ThemHangHoaForm_Load(object sender, EventArgs e)
        {
            loadData();
        }

        private void loadData()
        {
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            var NhomHangHoas = from ig in dc.NhomHangHoas
                             where ig.deleted == false
                             select ig;
            foreach (_DB.NhomHangHoa ig in NhomHangHoas)
            {
                comboBoxGroup.Items.Add(ig);
            }
            
            // Edit mode
            if (_maHangHoa != "")
            {
                _DB.HangHoa HangHoa = (from it in dc.HangHoas
                                 where it.MaHangHoa == _maHangHoa
                                 select it).Single();

                txtName.Text = HangHoa.TenHangHoa;
                txtUnit.Text = HangHoa.DonVi;
                txtMeasure.Text = HangHoa.Item_Measure;
                txtPurpose.Text = HangHoa.MucDich;
                txtDescription.Text = HangHoa.MoTa;
                txtStatus.Text = HangHoa.TrangThai;
                txtNote.Text = HangHoa.GhiChu;
                txtGia.Text = HangHoa.Gia.ToString();
                txtMaHangHoa.Text = HangHoa.MaHangHoa;
                txtMaHangHoa.ReadOnly = true;

                comboBoxGroup.SelectedItem = HangHoa.NhomHangHoa;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (txtName.Text.Equals(""))
            {
                MessageBox.Show("Name is empty!");
                return;
            }

            try
            {
                _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
                _DB.HangHoa HangHoa;

                if (_maHangHoa == "")
                    HangHoa = new _DB.HangHoa();
                else
                    HangHoa = (from it in dc.HangHoas
                            where it.MaHangHoa == _maHangHoa
                            select it).Single();

                HangHoa.TenHangHoa = txtName.Text;
                HangHoa.DonVi = txtUnit.Text;
                HangHoa.Item_Measure = txtMeasure.Text;
                HangHoa.MucDich = txtPurpose.Text;
                HangHoa.MoTa = txtDescription.Text;
                HangHoa.TrangThai = txtStatus.Text;
                HangHoa.GhiChu = txtNote.Text;
                HangHoa.MaHangHoa = txtMaHangHoa.Text;
                HangHoa.Gia = float.Parse(txtGia.Text);

                _DB.NhomHangHoa ig = (_DB.NhomHangHoa)comboBoxGroup.SelectedItem;
                if (ig != null)
                    HangHoa.NhomHangHoa_ID = ig.NhomHangHoa_ID;
                
                if (_maHangHoa == "")
                    dc.HangHoas.InsertOnSubmit(HangHoa);

                dc.SubmitChanges();
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
